﻿package com.yy.ui.containers {
    import flash.display.*;

    public class LayoutContainer extends ContainerBase {

        public static const LAYOUT_HORIZONTAL:String = "horizontal";
        public static const LAYOUT_VERTICAL:String = "vertical";

        private var _layout:String = "horizontal";
        private var _distance:int = 20;
        private var _itemDistance:int = 15;
        private var itemContainer:Sprite;

        public function LayoutContainer(){
            this.itemContainer = new Sprite();
            super.addChild(this.itemContainer);
        }
        override public function addChild(_arg1:DisplayObject):DisplayObject{
            this.itemContainer.addChild(_arg1);
            return (_arg1);
        }
        override public function addChildAt(_arg1:DisplayObject, _arg2:int):DisplayObject{
            this.itemContainer.addChildAt(_arg1, _arg2);
            return (_arg1);
        }
        override public function removeChild(_arg1:DisplayObject):DisplayObject{
            return (this.itemContainer.removeChild(_arg1));
        }
        override public function removeChildAt(_arg1:int):DisplayObject{
            return (this.itemContainer.removeChildAt(_arg1));
        }
        override public function removeAllChildren():void{
            while (this.itemContainer.numChildren) {
                this.itemContainer.removeChildAt(0);
            };
        }
        public function updateLayout():void{
            var _local2:DisplayObject;
            var _local3:DisplayObject;
            var _local1:int;
            if (this.layout == LayoutContainer.LAYOUT_HORIZONTAL){
                while (_local1 < this.itemContainer.numChildren) {
                    _local2 = (this.itemContainer.getChildAt(_local1) as DisplayObject);
                    if (_local1 > 0){
                        _local3 = (this.itemContainer.getChildAt((_local1 - 1)) as DisplayObject);
                        if (_local2.visible){
                            _local2.x = ((_local3.x + _local3.width) + this.itemDistance);
                        } else {
                            _local2.x = _local3.x;
                        };
                    };
                    _local2.y = ((this.height - _local2.height) / 2);
                    _local1++;
                };
                if (this.itemContainer.numChildren){
                    this.itemContainer.x = ((this.width - this.itemContainer.width) / 2);
                };
            } else {
                if (this.layout == LayoutContainer.LAYOUT_VERTICAL){
                    while (_local1 < this.itemContainer.numChildren) {
                        _local2 = (this.itemContainer.getChildAt(_local1) as DisplayObject);
                        if (_local1 > 0){
                            _local3 = (this.itemContainer.getChildAt((_local1 - 1)) as DisplayObject);
                            if (_local2.visible){
                                _local2.y = ((_local3.y + _local3.height) + this.itemDistance);
                            } else {
                                _local2.y = _local3.y;
                            };
                        };
                        _local2.x = 0;
                        _local1++;
                    };
                    if (this.itemContainer.numChildren){
                        this.height = (this.itemContainer.height + (this.distance * 2));
                        this.itemContainer.y = this.distance;
                    };
                };
            };
        }
        public function get distance():int{
            return (this._distance);
        }
        public function set distance(_arg1:int):void{
            this._distance = _arg1;
            this.updateLayout();
        }
        public function get itemDistance():int{
            return (this._itemDistance);
        }
        public function set itemDistance(_arg1:int):void{
            this._itemDistance = _arg1;
            this.updateLayout();
        }
        public function get layout():String{
            return (this._layout);
        }
        public function set layout(_arg1:String):void{
            this._layout = _arg1;
            this.updateLayout();
        }

    }
}
